#include <iostream>
#include <vector>
#include <string>
#include <math.h>

using namespace std;

int myAtoi(string s) {
    long long int ret = 0; int i = 0, signal = 1; vector<int> nums;
    // 跳过空格
    while(s[i] == ' ' && i <= s.size()) i++;
    // 判断正负
    if(s[i] == '-'){
        signal = -1;
        i++;
    }else if(s[i] == '+'){
        i++;
    }
    // 跳过0
    while(s[i] == '0' && i <= s.size()) i++;
    // 读取数字
    while(s[i] >= '0' && s[i] <= '9' && i <= s.size()){
        nums.push_back(s[i++]-'0');
    }
    for(int j = 0; j < nums.size(); j++){
        ret = 10 * ret + nums[j];
        ret = signal == 1 ? min(ret, (long long)INT_MAX) : min(ret, -(long long)INT_MIN);
    }
    return signal * ret;
}

int main(){
    string s = "   -423 with words";
    cout << myAtoi(s) << endl;
    return 0;
}
